/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mylhyl.circledialog.sample.provider;

import com.mylhyl.circledialog.sample.ResourceTable;
import com.mylhyl.circledialog.sample.model.ListCheckEntry;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Checkbox;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * 类功能描述(类上)
 *
 * @since 2021-07-27
 */
public class RvListChooseProvider extends BaseItemProvider {
    private List<ListCheckEntry> entry = new ArrayList<>();
    private Context context;
    private ListCheckEntry chooseOne = new ListCheckEntry();

    /**
     * RvListChooseProvider
     *
     * @param entry entry
     * @param context context
     */
    public RvListChooseProvider(List<ListCheckEntry> entry, Context context) {
        this.entry = entry;
        this.context = context;
    }

    @Override
    public int getCount() {
        return entry.size();
    }

    @Override
    public Object getItem(int i) {
        return entry.get(i);
    }

    @Override
    public long getItemId(int id) {
        return id;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        ViewHolder viewHolder;
        if (component == null) {
            cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_rv_list_choose, null, false);
            viewHolder = new ViewHolder(cpt);
            cpt.setTag(viewHolder);
        } else {
            cpt = component;
            viewHolder = (ViewHolder) cpt.getTag();
        }
        viewHolder.text.setText(entry.get(i).getItemName());
        viewHolder.checkbox.setChecked(entry.get(i).isCheck());
        return cpt;
    }

    /**
     * 更新数据
     *
     * @param data 传入的数据
     */
    public void updateData(List<ListCheckEntry> data) {
        entry.clear();
        entry.addAll(data);
        notifyDataChanged();
    }

    /**
     * 置已选数据
     *
     * @param isMany isMany
     * @param position position
     */
    public void setChoose(boolean isMany, int position) {
        if (isMany) {
            entry.get(position).setCheck(!entry.get(position).isCheck());
            notifyDataChanged();
        } else {
            for (int in = 0; in < entry.size(); in++) {
                if (in == position) {
                    entry.get(in).setCheck(true);
                    chooseOne = entry.get(position);
                } else {
                    entry.get(in).setCheck(false);
                }
            }
            notifyDataChanged();
        }
    }

    /**
     * 获取单选值
     *
     * @return String
     */
    public String getChooseOne() {
        if (!"".equals(chooseOne.getItemName()) && chooseOne.getItemName() != null) {
            return chooseOne.getItemName();
        } else {
            return "";
        }
    }

    /**
     * 获取多选值
     *
     * @return String
     */
    public String getChooseMany() {
        StringBuilder returnstr = new StringBuilder();
        for (int in = 0; in < entry.size(); in++) {
            if (entry.get(in).isCheck()) {
                returnstr.append(in + "=" + entry.get(in).getItemName() + ";");
            }
        }
        return returnstr.toString();
    }

    /**
     * ViewHolder
     *
     * @since 2021-07-27
     */
    public static class ViewHolder {
        private Text text;
        private Checkbox checkbox;

        ViewHolder(Component component) {
            text = (Text) component.findComponentById(ResourceTable.Id_item_list_choose_name);
            checkbox = (Checkbox) component.findComponentById(ResourceTable.Id_item_list_choose_check);
        }
    }
}
